package io.aicn.lowcode.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.aicn.lowcode.entity.Employee;
import io.aicn.lowcode.mapper.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmployeeService extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {
    
    @Autowired
    private EmployeeMapper employeeMapper;
    
    public List<Employee> searchEmployees(String employeeNumber, String firstName, String lastName, String email, 
                                         String department, String position, String status, BigDecimal minSalary, BigDecimal maxSalary) {
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        
        if (StringUtils.hasText(employeeNumber)) {
            queryWrapper.like(Employee::getEmployeeNumber, employeeNumber);
        }
        if (StringUtils.hasText(firstName)) {
            queryWrapper.like(Employee::getFirstName, firstName);
        }
        if (StringUtils.hasText(lastName)) {
            queryWrapper.like(Employee::getLastName, lastName);
        }
        if (StringUtils.hasText(email)) {
            queryWrapper.like(Employee::getEmail, email);
        }
        if (StringUtils.hasText(department)) {
            queryWrapper.eq(Employee::getDepartmentId, department);
        }
        if (StringUtils.hasText(position)) {
            queryWrapper.eq(Employee::getPositionId, position);
        }
        if (StringUtils.hasText(status)) {
            queryWrapper.eq(Employee::getStatus, status);
        }
        if (minSalary != null) {
            queryWrapper.ge(Employee::getSalary, minSalary);
        }
        if (maxSalary != null) {
            queryWrapper.le(Employee::getSalary, maxSalary);
        }
        
        return employeeMapper.selectList(queryWrapper);
    }
    
    public Employee createEmployee(Employee employee) {
        employee.setCreatedAt(LocalDateTime.now());
        employee.setUpdatedAt(LocalDateTime.now());
        
        employeeMapper.insert(employee);
        return employee;
    }
    
    public Employee updateEmployee(Long id, Employee employee) {
        Employee existingEmployee = employeeMapper.selectById(id);
        if (existingEmployee == null) {
            return null;
        }
        
        if (employee.getEmployeeNumber() != null) {
            existingEmployee.setEmployeeNumber(employee.getEmployeeNumber());
        }
        if (employee.getFirstName() != null) {
            existingEmployee.setFirstName(employee.getFirstName());
        }
        if (employee.getLastName() != null) {
            existingEmployee.setLastName(employee.getLastName());
        }
        if (employee.getEmail() != null) {
            existingEmployee.setEmail(employee.getEmail());
        }
        if (employee.getPhone() != null) {
            existingEmployee.setPhone(employee.getPhone());
        }
        if (employee.getGender() != null) {
            existingEmployee.setGender(employee.getGender());
        }
        if (employee.getBirthDate() != null) {
            existingEmployee.setBirthDate(employee.getBirthDate());
        }
        if (employee.getHireDate() != null) {
            existingEmployee.setHireDate(employee.getHireDate());
        }
        if (employee.getDepartmentId() != null) {
            existingEmployee.setDepartmentId(employee.getDepartmentId());
        }
        if (employee.getPositionId() != null) {
            existingEmployee.setPositionId(employee.getPositionId());
        }
        if (employee.getManagerId() != null) {
            existingEmployee.setManagerId(employee.getManagerId());
        }
        if (employee.getSalary() != null) {
            existingEmployee.setSalary(employee.getSalary());
        }
        if (employee.getStatus() != null) {
            existingEmployee.setStatus(employee.getStatus());
        }
        if (employee.getAddress() != null) {
            existingEmployee.setAddress(employee.getAddress());
        }
        if (employee.getEmergencyContact() != null) {
            existingEmployee.setEmergencyContact(employee.getEmergencyContact());
        }
        if (employee.getEmergencyPhone() != null) {
            existingEmployee.setEmergencyPhone(employee.getEmergencyPhone());
        }
        if (employee.getNotes() != null) {
            existingEmployee.setNotes(employee.getNotes());
        }
        
        existingEmployee.setUpdatedAt(LocalDateTime.now());
        employeeMapper.updateById(existingEmployee);
        return existingEmployee;
    }
    
    public boolean deleteEmployee(Long id) {
        return employeeMapper.deleteById(id) > 0;
    }
}